#include "set.h"

/*-----------------------------------------------------------------*/
/* addSetHead - add item to head of linked list                    */
/*-----------------------------------------------------------------*/
void *
addSetHead (set ** list, void *item)
{
  set *lp = newSet ();

  lp->item = item;
  lp->next = *list;

  assert (lp != lp->item);
  *list = lp;
  return item;

}

/*------------------------------------------------------------------*/
/* getSet - will delete & return the first item from the set		*/
/*------------------------------------------------------------------*/
void *
getSet (set ** list)
{
  set *lp;
  void *item;

  /* if list is empty then we cannot delete */
  if (*list == NULL)
    return (void *) NULL;


  /* find the item in the list */
  lp = *list;
  item = lp->item;		/* save the item */

  *list = lp->next;
  return item;
}

/*-----------------------------------------------------------------*/
/* addSet - add an item to a linear linked list                    */
/*-----------------------------------------------------------------*/
void *
addSet (set ** list, void *item)
{
  set *lp;

  /* item added to the tail of the list */

  /* if the list is empty */
  if (*list == NULL)
    {
      lp = *list = newSet ();
    }
  else
    {
      /* go to the end of the list */
      for (lp = *list; lp->next; lp = lp->next);
      lp = lp->next = newSet ();
    }

  /* lp now all set */
  lp->item = item;

  return item;
}

/*-----------------------------------------------------------------*/
/* setNextItem - gets the next item, changes the iteration         */
/*-----------------------------------------------------------------*/
void *
setNextItem (set * sset)
{
  if (sset && sset->curr)
    {
      sset->curr = sset->curr->next;
      if (sset->curr)
	return sset->curr->item;
    }
  return NULL;
}

/*-----------------------------------------------------------------*/
/* newSet - will allocate & return a new set entry                 */
/*-----------------------------------------------------------------*/
set *
newSet (void)
{
  set *lp;

  lp = Safe_alloc (sizeof (set));
  lp->item = lp->curr = lp->next = NULL;
  return lp;
}


/*-----------------------------------------------------------------*/
/* deleteSetItem - will delete a given item from the list          */
/*-----------------------------------------------------------------*/
void
deleteSetItem (set ** list, void *item)
{
	set *lp, *lp1;

	/* if list is empty */
	if (*list == NULL)
		return;

	/* if this item is at the head of the list */
	if ((*list)->item == item)
	{
		lp = *list;
		*list = (*list)->next;
		Safe_free (lp);
		return;
	}

	/* find the item in the list */
	for (lp = *list; lp->next; lp = lp->next)
	{
		if (lp->next->item == item)   /* the next one is it */
		{
			lp1 = lp->next;             /* this one will need to be freed */
			lp->next = lp->next->next;  /* take out of list */
			Safe_free (lp1);
			return;
		}
	}

	/* could not find it */
}
